package com.neusoft.NursingHome.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.NursingHome.po.Meal;
import com.neusoft.NursingHome.po.ResponseBean;
import com.neusoft.NursingHome.service.MealService;

@RestController
@RequestMapping("/meal")
public class MealController {

    @Autowired
    private MealService mealService;


    /**
     * 添加新的食物
     * @param meal 食物对象
     * @return 添加结果的 ResponseBean
     */
    @PostMapping("/add")
    public ResponseBean<Boolean> addMeal(@RequestBody Meal meal) {
        boolean result = mealService.addMeal(meal);
        return new ResponseBean<>(result);
    }

    /**
     * 删除食物（逻辑删除）
     * @param mealId 食物的唯一标识符
     * @return 删除结果的 ResponseBean
     */
    @DeleteMapping("/delete/{mealId}")
    public ResponseBean<Boolean> deleteMeal(@PathVariable Integer mealId) {
        boolean result = mealService.deleteMeal(mealId);
        return new ResponseBean<>(result);
    }

    /**
     * 切换食物的状态
     * @param mealId 食物的唯一标识符
     * @return 切换状态结果的 ResponseBean
     */
    @PutMapping("/status/{mealId}")
    public ResponseBean<Boolean> updateMealStatus(@PathVariable Integer mealId) {
        boolean result = mealService.updateMealStatus(mealId);
        return new ResponseBean<>(result);
    }
    /**
     * 按照状态查询食物
     * @param status 状态值
     * @return 包含符合条件的食物列表的 ResponseBean
     */
    @GetMapping("/status/{status}")
    public ResponseBean<List<Meal>> getMealsByStatus(@PathVariable Integer status) {
        List<Meal> meals = mealService.getMealsByStatus(status);
        return new ResponseBean<>(meals);
    }

    /**
     * 按照食品名称模糊查询并结合状态筛选
     * @param name 食品名称（支持模糊匹配）
     * @param status 状态值
     * @return 包含符合条件的食物列表的 ResponseBean
     */
    @GetMapping("/searchByNameAndStatus")
    public ResponseBean<List<Meal>> searchMealsByNameAndStatus(
            @RequestParam String name,
            @RequestParam Integer status) {
        List<Meal> meals = mealService.searchMealsByNameAndStatus(name, status);
        return new ResponseBean<>(meals);
    }
}